package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategory2;
import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@RefreshScope // 刷新配置文件
public class ManageController {

    @Autowired
    private ManageService manageService;

    @Value("${minio.endpointUrl}")
    private String url;

    // admin/product/getCategory1
    // 获取一级分类数据
    @GetMapping("getCategory1")
    public Result getCategory1(){

        System.out.println("minio url:\t"+url);

        // 返回集合数据
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    // product/getCategory2/{category1Id}
    // 获取二级分类数据
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){

        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);

        return Result.ok(baseCategory2List);
    }

    // admin/product/getCategory3/{category2Id}
    // 获取二级分类数据
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){

        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);

        return Result.ok(baseCategory3List);
    }

    // 根据分类Id获取平台属性数据
    // product/attrInfoList/{category1Id}/{category2Id}/{category3Id}

    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = manageService.getAttrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    //  /admin/product/saveAttrInfo
    // 修改或者保存平台属性
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //  调用服务层方法
        if (baseAttrInfo!=null){
            this.manageService.saveAttrInfo(baseAttrInfo);
            //  默认返回 成功！
            return Result.ok();
        }else {
            //  默认返回 失败
            return Result.fail();
        }
    }

    //  /admin/product/getAttrValueList/{attrId}
    // 通过平台属性ID获取属性值
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //  select * from base_attr_value where attrId = ?
        //  从业务完整性的角度讲：
        BaseAttrInfo baseAttrInfo =  this.manageService.getBaseAttrInfo(attrId);
        //  List<BaseAttrValue> baseAttrValueList = this.manageService.getAttrValueList(attrId);
        //  返回属性中的属性值集合！
        return Result.ok(baseAttrInfo.getAttrValueList());
    }


}
